local chaozheng = fk.CreateSkill {
  name = "chaozheng",
}

chaozheng:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Start and
      not table.every(player.room:getOtherPlayers(player, false), function(p) return p:isKongcheng() end)
  end,
  on_cost = function(self, event, target, player, data)   
    return player.room:askToSkillInvoke(player,{skill_name=self.name,prompt="#chaozheng-invoke"})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player, false), function(p) return not p:isKongcheng() end)
    if #targets == 0 then return end
    room:doIndicate(player.id, table.map(targets, Util.IdMapper))
    local discussion = U.Discussion(player, targets, self.name)
    if discussion.color == "red" then
      for _, p in ipairs(targets) do
        if p:isWounded() and not p.dead and discussion.results[p.id].opinion == "red" then
          room:recover({
            who = p,
            num = 1,
            recoverBy = player,
            skillName = self.name,
          })
        end
      end
    elseif discussion.color == "black" then
      for _, p in ipairs(targets) do
        if not p.dead and discussion.results[p.id].opinion == "red" then
          room:loseHp(p, 1, self.name)
        end
      end
    end
    if not player.dead and table.every(targets, function(p)
      return discussion.results[p.id].opinion == discussion.results[targets[1].id].opinion end) then
      player:drawCards(#targets, self.name)
    end
  end,
})

return chaozheng